
### soft location
MAVEN_HOME=/usr/local/maven
export SERVER_HOME=/usr/local/tomcat

### environment

APP_BRANCH=master
APP_NAME=bigsyb-all
GIT_URL=git@git.oschina.net:unsuccessful/bigsyb.git

APPS_BASE_HOME=$HOME/apps
ROJECT_SOURCE_DIR=$APPS_BASE_HOME/$APP_NAME
ROJECT_SOURCE_BRANCH_DIR=$ROJECT_SOURCE_DIR/$APP_BRANCH
WEB_APP_HOME=$ROJECT_SOURCE_BRANCH_DIR/$APP_NAME/target

export CATALINA_HOME=$SERVER_HOME
export CATALINA_BASE=$HOME/.tomcat

################################Update source code & package####################
package(){

    ###update source code

	echo "package 1"
	
	## always remove old code 
    echo " start to remove existed src folder:  $ROJECT_SOURCE_BRANCH_DIR "
    rm -rf  $ROJECT_SOURCE_BRANCH_DIR
	
	echo "package 2"
    
    echo "[INFO] git clone -b $APP_BRANCH $GIT_URL $APP_BRANCH"
    if [ ! -d "$ROJECT_SOURCE_DIR" ]; then
    	if [ ! -d "$APPS_BASE_HOME" ]; then
	        echo "[INFO] make APPS_BASE_HOME :" $APPS_BASE_HOME
	        mkdir $APPS_BASE_HOME
    	fi
    	if [ ! -d "$ROJECT_SOURCE_DIR" ]; then
	        echo "[INFO] make ROJECT_SOURCE_DIR :" $ROJECT_SOURCE_DIR
	        mkdir $ROJECT_SOURCE_DIR
    	fi
    fi

    cd $ROJECT_SOURCE_DIR
    git clone -b $APP_BRANCH $GIT_URL $APP_BRANCH

    ###package app
    echo "[INFO] start to package application..."
    echo "current path is " `pwd`
    echo "result cmd is $MAVEN_HOME/bin/mvn package"
    cd $ROJECT_SOURCE_BRANCH_DIR/$APP_NAME
    $MAVEN_HOME/bin/mvn package
    echo "[INFO] package application finish."

}

### stop tomcat

get_tomcat_pid(){    
    STR=`ps -C java -f --width 1000 | grep "$TOMCAT_BASE_DIR"|awk '{print $2}'`           
    echo $STR
}

stop_tomcat(){
    TOMCAT_JAVA_PID=`get_tomcat_pid`
    if [ ! -z "$TOMCAT_JAVA_PID" ] ; then
        echo -e "[INFO] kill java process $TOMCAT_JAVA_PID .\c"      
        kill -9 $TOMCAT_JAVA_PID > /dev/null 2>&1
        echo "Kill tomcat Oook!"
    else
        echo "[WARN] $HOST_NAME: tomcat not running, who care?"
    fi
}

################################################################
##check if started before
check_server(){
    java_pid=`ps  --no-heading -C java -f --width 1000 | grep "$$CATALINA_BASE" |awk '{print $2}'`
    if [ ! -z "$java_pid" ]; then
        echo "[INFO] Tomcat server already started: pid=$java_pid"
        exit;
    fi
}

copy_server_home(){
    if [ -d "$CATALINA_BASE" ]; then
        rm -rf $CATALINA_BASE
    fi
    mkdir -p $CATALINA_BASE/conf
    mkdir -p $CATALINA_BASE/webapps
	mkdir -p $CATALINA_BASE/logs
	mkdir -p $CATALINA_BASE/temp
	touch $CATALINA_BASE/logs/catalina.out
}

copy_tomcat_conf(){
    cp -rf $CATALINA_HOME/conf/. $CATALINA_BASE/conf/.
}

copy_war(){
    # cp -r $WEB_APP_HOME/$APP_NAME-war $CATALINA_BASE/webapps
    if [ ! -d "$CATALINA_BASE/webapps/ROOT" ]; then
        echo "[INFO] make ROOT :" $CATALINA_BASE/webapps/ROOT
        mkdir $CATALINA_BASE/webapps/ROOT
    fi
    cp -rf $WEB_APP_HOME/$APP_NAME-1.0-SNAPSHOT/. $CATALINA_BASE/webapps/ROOT/.
}

deploy_to_tomcat(){
    check_server
    copy_server_home
    copy_tomcat_conf
    copy_war
}



main(){
    package
    stop_tomcat
	deploy_to_tomcat
	$CATALINA_HOME/bin/catalina.sh start > /dev/null
	tail -f $CATALINA_BASE/logs/catalina.out
}

main
